<?php

class My_Ajax_Response
{
    const STATUS_ERROR = 'ERROR';
    const STATUS_SUCCESS = 'SUCCESS';
    
    /**
     * @var string 
     */
    protected $id;
    /**
     * @var string 
     */
    protected $status;
    /**
     * @var mixed 
     */
    protected $data;
    /**
     * @var array 
     */
    protected $errors;
    
    protected function __construct($id)
    {
        $this->id = $id;
        $this->status = 'ERROR';
        $this->data = '';
        $this->errors = array();
    }
    
    /**
     * @return My_Ajax_Response
     */
    public static function factory($id = null)
    {
        if (is_null($id)) $id = uniqid ();
        return new self($id); 
    }
    
    /**
     * @return string 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * @param string $id
     * @return My_Ajax_Response 
     */
    public function setId($id)
    {
        $this->id = $id;
        return $this;
    }

        
    /**
     * @return string 
     */
    public function getStatus()
    {
        return $this->status;
    }

    /**
     * @param string $status
     * @return My_Ajax_Response 
     */
    public function setStatus($status)
    {
        $this->status = $status;
        return $this;
    }

    /**
     * @return mixed 
     */
    public function getData()
    {
        return $this->data;
    }

    /**
     * @param mixed $data
     * @return My_Ajax_Response 
     */
    public function setData($data)
    {
        $this->data = $data;
        return $this;
    }

    /**
     * @return array 
     */
    public function getErrors()
    {
        return $this->errors;
    }

    /**
     * @param array $errors
     * @return My_Ajax_Response 
     */
    public function setErrors($errors)
    {
        $this->errors = $errors;
        return $this;
    }
    
    /**
     * @param string $error
     * @param mixed $errorId (integer | string)
     * @return My_Ajax_Response 
     */
    public function addError($error, $errorId = null)
    {
        if (!is_null($errorId))
        {
            $this->errors[$errorId] = $error;
        }
        else
        {
            $this->errors[] = $error;
        }
        return $this;
    }  
    
    /**
     * @return void 
     */
    public function send()
    {
        $response = array('id'=>$this->getId(), 'status'=>$this->getStatus(), 'data'=>$this->getData(), 'errors'=>$this->getErrors());
        echo Zend_Json::encode($response);
    }
    
}